package com.toubutu.audio.auth.controller;

import com.toubutu.anno.Exclude;
import com.toubutu.anno.LoginRequired;
import com.toubutu.audio.admin.dto.LoginDto;
import com.toubutu.audio.admin.dto.RegisterDto;
import com.toubutu.audio.admin.vo.UserAuthVo;
import com.toubutu.audio.auth.service.UserAuthService;
import com.toubutu.audio.dto.ChangeBindEmailDto;
import com.toubutu.audio.dto.UpdatePasswordDto;
import com.toubutu.audio.dto.UserProfileDto;
import com.toubutu.common.Result;
import com.toubutu.entity.TokenEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 戒躁
 * @version 1.0
 * @date 2023年10月30日 15:52
 */
@RestController
@Api(tags = "用户认证接口")
@CrossOrigin
@RequestMapping("/auth")
@LoginRequired
public class UerAuthController {
    @Resource
    private UserAuthService userAuthService;

    @PostMapping("/login")
    @ApiOperation("账号登录")
    @Exclude
    public Result<TokenEntity> passwordLogin(@RequestBody LoginDto loginDto){
        return Result.ok(userAuthService.passwordLogin(loginDto));
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    @Exclude
    public Result<Void> register(@RequestBody RegisterDto registerDto){
        userAuthService.userRegister(registerDto);
        return Result.ok();
    }

    @GetMapping("/info")
    @ApiOperation("获取登录用户信息")
    public Result<UserAuthVo> getLoginUserInfo(@RequestHeader(name = "accessToken")String accessToken,
                                               @RequestHeader(name = "from",required = false)String from){
        return Result.ok(userAuthService.getLoginUserInfo(accessToken,from));
    }

    @PostMapping("/update/profile")
    @ApiOperation("用户更改个人信息")
    public Result<Void> updateUserProfile(@RequestBody UserProfileDto profileDto){
        userAuthService.updateUserInfo(profileDto);
        return Result.ok();
    }

    @GetMapping("/logout")
    @ApiOperation("用户退出登录")
    public Result<Void> logout(@RequestHeader(name = "accessToken")String accessToken){
        userAuthService.logout(accessToken);
        return Result.ok();
    }

    @PostMapping("/bind/email")
    @ApiOperation("更改绑定邮箱")
    public Result<Void> changeBindEmail(@RequestBody ChangeBindEmailDto bindEmailDto){
        userAuthService.updateBindEmail(bindEmailDto);
        return Result.ok();
    }
    @PostMapping("/update/pwd")
    @ApiOperation("更改密码")
    public Result<Void> changePassword(@RequestBody UpdatePasswordDto updatePasswordDto){
        userAuthService.updatePassword(updatePasswordDto);
        return Result.ok();
    }
}
